'''
Created on Dec 28, 2011

@author: onetera
'''
from PyQt4 import QtCore , QtGui
from lib.papyDB import papyDB , tableInfo
from lib.papyDBTable import *
from lib import globalVar as GV
import pickle
import os

class toolDataHandler:
    def __init__(self , toolName , userID):
        if GV.testUserID == '' : self.userID = userID
        else                   : self.userID = int(GV.testUserID)
#        self.userID = userID
        self.toolName = toolName
        
    def PickleLoads(self ):        
        pickData = TOOLS_INFO.search( 'data' , userID = self.userID , toolName = self.toolName )
        if pickData == None or str( pickData ) == '' or pickData == [] :
            return  appInfo()
        else : 
            return pickle.loads( str( pickData ) )
           
    def setPickleData(self , data ):                         
        pickleData = pickle.dumps( data )    
            
        if TOOLS_INFO.getFetch( "select count(*) from TOOLS_INFO where userID='%s' and toolName='%s'" % (self.userID , self.toolName) )[0][0]:
            TOOLS_INFO.update( 'data' , str(pickleData) , userID=self.userID, toolName=self.toolName )
        else :                        
            query = 'insert into TOOLS_INFO(userID,toolName,data) values("%s","%s","%s")' % (self.userID , self.toolName , pickleData)
            papyDB.query(query)
##        result = TOOLS_INFO.search( 'data' , userID = self.userID , toolName = self.toolName )      
##        if result != []:                                           
#        if TOOLS_INFO.getFetch( "select count(*) from TOOLS_INFO where userID='%s' and toolName='%s'" % (self.userID,self.toolName) )[0][0]:
#            TOOLS_INFO.update( 'data' , str( pickleData ) , userID = self.userID , toolName = self.toolName )
#        else :                        
#            TOOLS_INFO.register( self.userID, self.toolName , str(pickleData) )
   
class appInfo:
    def __init__(self ):             
        self.pos = [0,0]
        self.size = [0,0]        
        self.appData = {}   

    def geometry(self):
        if self.pos[0] < 0 :
            self.pos[0] = 100
        if  self.pos[1] < 0 :
            self.pos[1] = 100
        return self.pos + self.size
                
    def setCustomInfo(self , **data ):        
        if data != None:
            for x in data:
                self.appData[ x ] = data[ x ]  
    def __getitem__(self , key):
        if key in self.appData.keys():
            return self.appData[key]
        else : 
            self.appData[key] = 0
            return 0

class Asset:
    def __init__(self, assetID , assetName  , assetStatus  , assetType  , assetDesc ):
        self.assetID = assetID
        self.assetName = QtCore.QString( assetName )        
        self.assetStatus = QtCore.QString( assetStatus )
        self.assetType = QtCore.QString( assetType )        
        if assetDesc == None :
            assetDesc = ''
        self.assetDesc = QtCore.QString( assetDesc )
        self.assetVersion = QtCore.QString(  )
        self.assetVersionList = []
        
        
#class Task:
#    def __init__( self , assetID , status , startDate , finalDate , endDate , description ):
#        self.assetID = ASSET.search('assetName' , assetID = assetID)
#        self.status = QtCore.QString( status )        
#        self.startDate = QtCore.QString( str(startDate) )
#        if finalDate == None :
#            finalDate = ''
#        self.finalDate = QtCore.QString( str(finalDate) )    
#        self.endDate = QtCore.QString( str(endDate) )        
#        if description == None :
#            description = ''
#        self.description = QtCore.QString( description )
#        print self.assetID , self.status , self.startDate , self.finalDate , self.endDate , self.description
        
class Task:
    def __init__( self , assetID , status , startDate , finalDate , endDate , description ):
        
        if GV.testUserID == '': self.userID = USER_INFO.search('userID' , userLogInName = os.environ['username'])
        else :                  self.userID = GV.testUserID
        
        self.userName = USER_INFO.search('userName' , userID = self.userID )
        self.deptID = USER_INFO.search('deptID' , userID = self.userID )
#        self.userName = USER_INFO.search('userName' , userLogInName = os.environ['username'])
#        self.deptID = USER_INFO.search('deptID' , userLogInName = os.environ['username'])
#        self.deptID = 2

        if self.deptID==2 or self.deptID==4 or self.deptID==5 :
            self.assetID = ASSET.search('assetName' , assetID = assetID)
        else :
            self.assetID = SHOT.search('shotName' , shotID = assetID)
            
        self.status = QtCore.QString( status )        
        self.startDate = QtCore.QString( str(startDate) )
        if finalDate == None :
            finalDate = ''
        self.finalDate = QtCore.QString( str(finalDate) )    
        self.endDate = QtCore.QString( str(endDate) )        
        if description == None :
            description = ''
        self.description = QtCore.QString( description )
#        print self.assetID , self.status , self.startDate , self.finalDate , self.endDate , self.description
       
class AssetDate:
    def __init__ (self, assetID, assetName, assetStatus, assetStartDate, assetEndDate, assetFinalDate, assetDesc):
        self.assetID = assetID
        self.assetName = QtCore.QString( assetName )
        self.assetStatus = QtCore.QString( assetStatus )  
        self.assetStartDate = QtCore.QString( assetStartDate )  
        self.assetEndDate = QtCore.QString( assetEndDate )  
        self.assetFinalDate = QtCore.QString( assetFinalDate )  
        if assetDesc == None :
            assetDesc = ''
        self.assetDesc = QtCore.QString( assetDesc )
        
class treeItem(object):
    def __init__(self , data  , parent = None  ):
        self.parentItem = parent
        self.itemData = data
        self.childItems = []
    
    def child(self, row):        
        return self.childItems[row]
    
    def childCount(self):
        return len( self.childItems )    
    
    def childNumber(self):
        if self.parentItem != None:
            return self.parentItem.childItems.index( self )
        return 0
    
    def columnCount(self):
        return 1
    
    def data( self ):
        return self.itemData
    
    def parent(self):
        return self.parentItem

    def insertChildren(self, position, count):
        if position < 0 or position > len(self.childItems):
            return False
        for row in range(count):
            data = None 
            item = treeItem(data, self)
            self.childItems.insert(position, item)
        return True
    
class PSSdata:    
    def __init__(self , prjID = 0 , prjName = '' , seqID = 0 , seqName = '' , shotID = 0 , shotName = '' ,userID = 0 , userName = '' ):
        self.prjID = prjID
        self.prjName = prjName
        self.seqID = seqID
        self.seqName = seqName
        self.shotID = shotID
        self.shotName = shotName
        self.userID = userID
        self.userName = userName        
        self.data = [    [self.prjID , self.prjName] , [ self.seqID , self.seqName ] , 
             [ self.shotID , self.shotName ] , [ self.userID , self.userName ]   ]
    
    def __str__(self):
        return str( self.data )
        
    def __setitem__( self , key , value ):
        if key == 'prjID':
            self.data[0][0] = value
        elif key == 'prjName':
            self.data[0][1] = value
        elif key == 'seqID':
            self.data[1][0] = value
        elif key == 'seqName' : 
            self.data[1][1] = value
        elif key == 'shotID' : 
            self.data[2][0] = value
        elif key == 'shotName' :
            self.data[2][1] = value
        elif key == 'userID' :
            self.data[3][0] = value
        elif key == 'userName':
            self.data[3][1] = value
        else : 
            return
            
    def __getitem__( self , key ):
        if key == 'prjID':
            return self.data[0][0] 
        elif key == 'prjName':
            return self.data[0][1] 
        elif key == 'seqID':
            return self.data[1][0] 
        elif key == 'seqName' : 
            return self.data[1][1] 
        elif key == 'shotID' : 
            return self.data[2][0] 
        elif key == 'shotName' :
            return self.data[2][1]
        elif key == 'userID' :
            return self.data[3][0]
        elif key == 'userName':
            return self.data[3][1] 
        else : 
            return
        
         
        
        
#        
                    
    
if __name__ == '__main__':
#    pssData = PSSdata()
#    pssData['prjID'] = 3
#    print pssData 
    
    TOOLS_INFO.register(  '2' , '3ds Max', 'data' )   
